package ca.kooki.codesnippet;

import junit.framework.Assert;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import ca.kooki.codesnippet.core.User;

// to get it to run, be sure to set
// -Djava.library.path=C:\apps\eclipse\configuration\org.eclipse.osgi\bundles\79\1\.cp

public class CodeSnippetPreferencePageTest {

  private static Display display;
  private Composite parent;
  private CodeSnippetPreferencePage preferencePage;
  
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    display = new Display();
    Activator.loadDefaultAsRequired();
  }

  @Before
  public void setUp() throws Exception {
    parent = new Shell(display);
    preferencePage = new CodeSnippetPreferencePage();
  }
  
  @After
  public void tearDown() throws Exception {
    parent.dispose();
    Activator.resetDefault();
  }
  
  @AfterClass
  public static void tearDownAfterClass() throws Exception {
    display.dispose();
  }
  
  @Test
  public void Initialize()
  {
    preferencePage.createContents(parent);
    Assert.assertEquals("Repository Location:",preferencePage.labelCodeSnippetFile.getText());
    Assert.assertEquals("Logged In As:", preferencePage.labelUser.getText());
    Assert.assertEquals("",preferencePage.labelStatusTitle.getText());
    Assert.assertEquals("Your repository is configured properly", preferencePage.labelStatus.getText());
  } 
  
  @Test
  public void InitializeNoUserSet()
  {
    preferencePage.createContents(parent);
    Assert.assertEquals(User.ANNONYMOUS, InMemoryCodeSnippetRepository.getDefault().getUser());
  }
  
  @Test
  public void LoadDefaults()
  {
    Activator.getDefault().getPreferences().setDefault("repository_file", "x");
    preferencePage.createContents(parent);
    Assert.assertEquals("x", preferencePage.inputCodeSnippetFile.getText());

    Activator.getDefault().getPreferences().setValue("repository_file", "y");
    preferencePage.createContents(parent);
    Assert.assertEquals("y", preferencePage.inputCodeSnippetFile.getText());
  } 
  
  @Test
  public void performDefaults()
  {
    Activator.getDefault().getPreferences().setDefault("repository_file", "temp.xml");
    Activator.getDefault().getPreferences().setDefault("user", "aforward");

    preferencePage.createContents(parent);
    preferencePage.inputCodeSnippetFile.setText("nottemp.xml");
    preferencePage.inputUser.setText("notaforward");
    preferencePage.labelStatus.setText("p");
    preferencePage.performDefaults();
    Assert.assertEquals("temp.xml", preferencePage.inputCodeSnippetFile.getText());
    Assert.assertEquals("aforward", preferencePage.inputUser.getText());
    Assert.assertEquals("Your repository is configured properly", preferencePage.labelStatus.getText());
  }
  
  @Test
  public void performOk()
  {
    preferencePage.createContents(parent);
    preferencePage.inputCodeSnippetFile.setText("y");
    preferencePage.inputUser.setText("z");
    preferencePage.labelStatus.setText("p");
    preferencePage.performOk();
    Assert.assertEquals("y", Activator.getDefault().getPreferences().getString("repository_file"));
    Assert.assertEquals("z", Activator.getDefault().getPreferences().getString("user"));
    Assert.assertEquals("Your repository is configured properly", preferencePage.labelStatus.getText());
  }
  
  
}
